package com.summer.safe.tag;


import com.summer.safe.utils.CollectionKit;
import com.summer.safe.utils.TagUtil;
import org.beetl.core.tag.GeneralVarTagBinding;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Radio标签
 */
public class RadioTag extends GeneralVarTagBinding {
    //数据集
    private final static String items = "items";
    //值
    private final static String value = "value";
    //id、名称
    private final static String path = "path";
    //实际值
    private final static String itemValue = "itemValue";
    //显示值
    private final static String itemLabel = "itemLabel";
    //是否禁用
    private final static String disabled = "disabled";
    //类型 单选or复选框
    private final static String type = "type";

    @Override
    public void render() {
        String tagName = (String) this.args[0];
        Map attrs = (Map) args[1];
        try {
            List<String> radioOptions = new ArrayList<>();
            Object itemsMap = attrs.get(items);
            String radio;
                ((Map<Object, Object>) TagUtil.formatData(itemsMap)).forEach((k, v) -> {
                    String radioInput ="<input type='"+attrs.get(type)+"' ";
                    if (null != attrs.get(value) && k.equals(attrs.get(value))) {
                        //是否禁用
                        String radioDisabled= (String) attrs.get(disabled);
                        radioInput+="name='"+attrs.get(path)+"' value='" + k + "' title=" + v + " ";
                        if(radioDisabled!=null&&!radioDisabled.equals("")){
                            radioInput+=" disabled";
                        }
                        radioInput+=" checked />";
                        radioOptions.add(radioInput);
                    } else {
                        radioInput+="name='"+attrs.get(path)+"' value='" + k + "' title=" + v + " />";
                        radioOptions.add(radioInput);
                    }

                });
             radio=CollectionKit.join(radioOptions, "");
            this.ctx.byteWriter.writeString(radio);
        } catch (IOException e) {

        }
    }



}
